package com.manxi.service.impl;

import com.manxi.context.BaseContext;
import com.manxi.dto.ShoppingCartDTO;
import com.manxi.entity.Dish;
import com.manxi.entity.Setmeal;
import com.manxi.entity.ShoppingCart;
import com.manxi.mapper.DishMapper;
import com.manxi.mapper.SetmealMapper;
import com.manxi.mapper.ShoppingCartMapper;
import com.manxi.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询该用户的购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断要添加到购物车的商品是否已存在
        if (list != null && list.size() > 0) {//已存在则更改商品数量操作,数量加1
            shoppingCart = list.get(0);

            //判断添加到购物车的商品是否是选择具体数量
            if (shoppingCartDTO.getNumber() == null){
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            }else{
                shoppingCart.setNumber(shoppingCart.getNumber() + shoppingCartDTO.getNumber());
            }
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {//不存在则执行插入操作

            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //菜品id不为空，则添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                //判断添加到购物车的商品是否是选择具体数量
                if (shoppingCartDTO.getNumber() == null){
                    shoppingCart.setNumber(1);
                }else{
                    shoppingCart.setNumber(shoppingCartDTO.getNumber());
                }

            } else {
                //添加的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);

            }

            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 获取购物车列表
     *
     * @return
     */
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();

        return shoppingCartMapper.list(shoppingCart);
    }


    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //设置查询条件
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 查询当前登录用户的购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            if (number == 1) {
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            } else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
